package com.tianqu.tms.www.bid.action;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.bid.actionform.BidCarImgInputForm;
import com.tianqu.tms.www.bid.viewhelper.BidCarImgInputViewHelper;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;

/**
 * 图片上传
 * @author liudianyong
 *
 */
public class BidCarImageModifyConfirmAction extends TmsAction {
	private Logger logger = Logger.getLogger(BidCarImageModifyConfirmAction.class);
	private static final String ITEMSPATH="ItemsPath";//获取项目路径
	// return
	public static String UPLOAD = "upload";
	public static String IMG1 = "1";
	public static String IMG2 = "2";
	public static String IMG3 = "3";
	public static String IMG4 = "4";
	public static String IMG5 = "5";
	public static String IMG6 = "6";
	public static String IMG7 = "7";
	public static String IMG8 = "8";
	public static String IMGCARD = "9";
	public static String IMGREPORT = "10";
	public static String IMG11 = "11";
	public static String IMG12 = "12";
	public static String IMG13 = "13";
	public static String IMG14 = "14";
	public static String IMG15 = "15";
	public static String IMG16 = "16";
	public static String IMG17 = "17";
	public static String IMG18 = "18";
	public static String IMG19 = "19";
	public static String IMG20 = "20";
	public static String IMG21 = "21";
	public static String IMG22 = "22";// 图片从8张增加到20张  modify by mufeiyu

	BidCarImgInputViewHelper view = new BidCarImgInputViewHelper();

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		// 文件路径
		BidCarImgInputForm ImForm = (BidCarImgInputForm) form;
		if (ImForm.getButton1() != null) {
			// 返回
			BidCarSerchInputViewHelper vie = (BidCarSerchInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			request.setAttribute(TmsConstants.VIEWHELP, vie);
			
		} else if (ImForm.getButton2() != null) {
			BidCarImgInputForm Forms= (BidCarImgInputForm) form;
			FormFile formfil = Forms.getImgPath();
			//判断图片是否正确
			if(imageAll(formfil)){
				  setErrorsToRequest(request, TmsConstants.I0015 ,null);
					request.setAttribute(TmsConstants.VIEWHELP,view);
		            return mapping.findForward(UPLOAD);	
			}else if(formfil.getFileSize() > 1048576){
				setErrorsToRequest(request, TmsConstants.I0018 ,null);
				request.setAttribute(TmsConstants.VIEWHELP,view);
	            return mapping.findForward(UPLOAD);	
			}
			// 上传
			String ImageName = System.currentTimeMillis() + ".gif";// 图片名称
			String images = ImagesUpLoad(form,ImageName);
			BidCarImgInputViewHelper view = new BidCarImgInputViewHelper();
			view.setImages(images);//页面图片名
			view.setImgPathidden(images);
			
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(UPLOAD);
		} else if (ImForm.getButton3() != null) {
			// 确认
			BidCarSerchInputViewHelper vie = (BidCarSerchInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			BidCarImgInputForm formpath = (BidCarImgInputForm) form;

			String imagesPath = formpath.getImages();
			if (vie.getButtionType().equals(IMG1)) {
				vie.setImages1(imagesPath);
			} else if (vie.getButtionType().equals(IMG2)) {
				vie.setImages2(imagesPath);
			} else if (vie.getButtionType().equals(IMG3)) {
				vie.setImages3(imagesPath);
			} else if (vie.getButtionType().equals(IMG4)) {
				vie.setImages4(imagesPath);
			} else if (vie.getButtionType().equals(IMG5)) {
				vie.setImages5(imagesPath);
			} else if (vie.getButtionType().equals(IMG6)) {
				vie.setImages6(imagesPath);
			} else if (vie.getButtionType().equals(IMG7)) {
				vie.setImages7(imagesPath);
			} else if (vie.getButtionType().equals(IMG8)) {
				vie.setImages8(imagesPath);
			} else if (vie.getButtionType().equals(IMGCARD)) {
				vie.setImages9(imagesPath);
			} else if (vie.getButtionType().equals(IMGREPORT)) {
				vie.setImages10(imagesPath);
			} else if (vie.getButtionType().equals(IMG11)) {
				vie.setImages11(imagesPath);
			} else if (vie.getButtionType().equals(IMG12)) {
				vie.setImages12(imagesPath);
			} else if (vie.getButtionType().equals(IMG13)) {
				vie.setImages13(imagesPath);
			} else if (vie.getButtionType().equals(IMG14)) {
				vie.setImages14(imagesPath);
			} else if (vie.getButtionType().equals(IMG15)) {
				vie.setImages15(imagesPath);
			} else if (vie.getButtionType().equals(IMG16)) {
				vie.setImages16(imagesPath);
			} else if (vie.getButtionType().equals(IMG17)) {
				vie.setImages17(imagesPath);
			} else if (vie.getButtionType().equals(IMG18)) {
				vie.setImages18(imagesPath);
			} else if (vie.getButtionType().equals(IMG19)) {
				vie.setImages19(imagesPath);
			} else if (vie.getButtionType().equals(IMG20)) {
				vie.setImages20(imagesPath);
			} else if (vie.getButtionType().equals(IMG21)) {
				vie.setImages21(imagesPath);
			} else if (vie.getButtionType().equals(IMG22)) {
				vie.setImages22(imagesPath);// 图片从8张增加到20张  modify by mufeiyu
			}
			vie.setButtionType("");
			uc.setAttribute(TmsConstants.VIEWHELP, vie);
			request.setAttribute(TmsConstants.VIEWHELP, vie);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	/**
	 * 图片上传到临时文件夹
	 * 
	 * @param form
	 */
	private String ImagesUpLoad(ActionForm form,String ImageName) {
		BidCarImgInputForm ImForm = (BidCarImgInputForm) form;
		FormFile formfil = ImForm.getImgPath();
		try {
			@SuppressWarnings("resource")
			String path =ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_AUCTION_IMAGE_PATCH_TMP_KEY));
			FileOutputStream fileOutput = new FileOutputStream(new File(path+ImageName));
			fileOutput.write(formfil.getFileData());
			fileOutput.flush();
			
			return ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_AUCTION_IMAGE_PATCH_TMP_KEY) + ImageName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/***
	 * 图片名称验证
	 */
	public boolean imageAll(FormFile file) {
		int i = 0;
		String type = file.getFileName();
		if(type.length()==0)return true;
		while (i != -1) {
			// 找到上传文件的类型的位置，这个地方的是'.'
			i = type.indexOf(".");
			/* 截取上传文件的后缀名,此时得到了文件的类型 */
			type = type.substring(i + 1).toLowerCase();
		}
		if (!type.equals("jpg") && !type.equals("jpeg")&& !type.equals("gif")){
			return true;
		}else{
			return false;
		}
	}
}
